<template>
  <base-layout>
    <template #searchPanel>
      <el-row>
        <el-col :span="6">
          <div class="lineHeight">职位</div>

          <ul class="listPanle">
            <li
              @click="projectDictCheck(item.id)"
              :style="item.isCheck ? ' color: #409eff' : ''"
              v-for="(item, index) in dict"
              :key="index"
              :value="item.id"
            >
              {{ item.roleName }}
            </li>
          </ul>
        </el-col>
        <el-col :span="18">
          <el-col :span="24" class="seachpanle">
            <div class="title lineHeight">员工</div>
            <div class="serach">
              <el-input
                v-model="form.user"
                clearable
                placeholder="员工姓名/编号/手机号"
              />
              <el-button icon="el-icon-search" type="primary" @click="query"
                >搜索</el-button
              >
            </div>
          </el-col>
          <el-col :span="24">
            <el-checkbox-group
              v-if="
                dataList[`roleId${form.roleId}`] &&
                dataList[`roleId${form.roleId}`].length > 0
              "
              v-model="checkboxGroup"
              class="rowcheckboxgroup"
            >
              <el-row
                v-for="item in dataList[`roleId${form.roleId}`]"
                :key="item.id"
              >
                <el-checkbox border :checked="item.isCheck" :label="item.id">
                  <el-row type="flex" justify="space-between">
                    <el-col :span="12">{{ item.code }}</el-col>
                    <el-col :span="12">{{ item.storeName }}</el-col>
                  </el-row>
                  <el-row type="flex" justify="space-between">
                    <el-col :span="12">{{ item.userName }}</el-col>
                    <el-col :span="12">{{ item.phone }}</el-col>
                  </el-row>
                </el-checkbox>
              </el-row>
            </el-checkbox-group>
            <div
              v-else
              style="
                text-align: center;
                padding: 40px;
                font-size: 14px;
                color: #606266;
              "
            >
              暂无数据
            </div>
          </el-col>
        </el-col>
      </el-row>
    </template>
    <template #contentPanel>
      <el-row>
        <el-col :span="22">已选员工</el-col>
      </el-row>
      <el-divider content-position="center"></el-divider>
      <el-row>
        <data-table
          :value="tableData"
          :total-count="tableData.length"
          :has-action="true"
          :table-column-attributes="tableColumnAttributes"
          :width="160"
          :localPage="true"
        >
          <template #action="{ row }">
            <i
              class="el-icon-delete"
              title="删除"
              style="color: red; margin-left: 20px; cursor: pointer"

              @click="deleteById(row)"
            ></i>
          </template>
        </data-table>
      </el-row>
      <el-row>
        <el-col :span="24" align="right" justify="center">
          <el-button type="success" @click="select">选择员工</el-button>
        </el-col>
      </el-row>
    </template>
  </base-layout>
</template>

<script>
import BaseLayout from '@/views/layout/Base'
import DataTable from '@/components/DataTable'
import { getDict, getStoreId } from '@/utils'
import { dataList as employeeDataList } from '@/api/after-sale/systemManage/role'
import { baseUserPageStore } from '@/api/after-sale/personnel/index'
export default {
  components: {
    'base-layout': BaseLayout,
    DataTable,
  },
  props: {
    storageId: {
      type: Number,
    },
  },
  data() {
    return {
      form: {
        storeId: '', //门店id
        roleId: '', //角色ID
        user: '', //用户相关信息
      },
      tableColumnAttributes: [
        {
          name: '员工编号',
          value: 'code',
          type: 'text',
        },
        {
          name: '所属门店',
          value: 'storeName',
          type: 'text',
        },
        {
          name: '员工姓名',
          value: 'userName',
          type: 'text',
        },
        {
          name: '员工电话',
          value: 'phone',
          type: 'text',
        },
        {
          name: '员工职位',
          value: 'roleName',
          type: 'text',
        },
      ],
      dict: [], //字典
      checkDictValue: {}, //选中字典
      dataList: {},
      checkboxGroup: [],
      tableData: [],
      totalCount: 0,
    }
  },
  watch: {
    checkboxGroup: function (value) {
      if (value) {
        this.addTable(value)
      }
    },
  },
  methods: {
    //项目类型选择 触发事件  更新选中的项目类型码值 以及搜索条件
    projectDictCheck(value) {
      if (value) {
        this.dict.forEach((item) => {
          if (value == item.id) {
            this.checkDictValue = item
            this.form.user = ''
            this.form.roleId = value
            item.isCheck = true
            this.query()
          } else {
            item.isCheck = false
          }
        })
      } else {
        this.form.user = ''
        this.checkDictValue = {}
        this.form.roleId = ''
        this.dict[0].isCheck = true
        this.query()
      }
    },
    // 删除
    deleteById(row) {
      this.checkboxGroup = this.checkboxGroup.map((e) => {
        if (e != row.id) {
          return e
        }
      })
    },
    async query() {
      this.form.roleId = this.checkDictValue.id
      this.form.storeId = getStoreId()
      let {
        data: { content },
      } = await baseUserPageStore(this.form, 100, 1)
      if (this.dataList[`roleId${this.form.roleId}`]) {
        this.$set(
          this.dataList,
          `roleId${this.form.roleId}`,
          this.checkData(content)
        )
      } else {
        this.$set(this.dataList, `roleId${this.form.roleId}`, content)
      }
    },
    //遍历元数据结构 赋值勾选
    checkData(content) {
      let _projectDictId = this.form.roleId
      this.dataList[`roleId${_projectDictId}`].forEach((item) => {
        if ('isCheck' in item && item.isCheck) {
          content.forEach((contentItem) => {
            if (contentItem.id == item.id) {
              contentItem.isCheck = true
            }
          })
        }
      })
      return content
    },
    // 选择配件
    select() {
      this.$emit('getList', this.tableData)
    },
    // 表格数据添加
    addTable(value) {
      this.tableData = []
      let data = []
      value.forEach((e) => {
        Object.keys(this.dataList).forEach((key) => {
          if (this.dataList[`${key}`].length > 0) {
            this.dataList[`${key}`].forEach((g) => {
              if (e === g.id) {
                data.push(g)
              }
            })
          }
        })
      })
      this.tableData = data
    },
  },
  async created() {
    //   storeId:getStoreId() TODO 角色这里是否需要 选择入职门店或当前选择门店
    let {
      data: { content },
    } = await employeeDataList({}, 100, 1)
    this.dict = [{ roleName: '全部', id: '' }, ...content]
  },
}
</script>

<style lang="scss" scoped>
.seachpanle {
  display: flex;

  .title {
    flex: 1;
  }

  .serach {
    width: 300px;
    display: flex;

    .el-button {
      margin-left: 10px;
    }
  }
}

.lineHeight {
  line-height: 36px;
}

.textAlign {
  text-align: center;
}

.listPanle::-webkit-scrollbar {
  display: none;
}
.listPanle {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 200px;
  overflow: auto;
  margin-right: 10px;
  border-right: 1px solid #bbbbbb;

  li {
    // border-top: 1px solid #bbbbbb;
    // border-bottom: 1px solid #bbbbbb;
    height: 34px;
    line-height: 34px;
    text-align: center;
    font-size: 16px;
    color: #606266;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  li:hover {
    cursor: pointer;
    background-color: #f5f7fa;
  }
}

.active {
  color: #409eff;
}

.mount {
  color: #f40;
}
/deep/ .el-checkbox {
  width: 100%;
  margin-top: 10px;
}
.el-checkbox.is-bordered.el-checkbox--medium {
  height: 100%;
  /deep/ .el-checkbox__label {
    width: 100%;
  }
  /deep/ .el-checkbox__input {
    margin-top: -20px;
  }
}
.rowcheckboxgroup::-webkit-scrollbar {
  display: none;
}
.rowcheckboxgroup {
  max-height: 200px;
  width: 100%;
  overflow: auto;
}
.userinfo {
  border: 1px solid red;
}
</style>
